<template>
	<view>
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回" right-icon="home" @clickRight="backWork"
			title="库存统计" @clickLeft="back" />
				
			<uni-search-bar placeholder="请输入连锁店名称" bgColor="#EEEEEE" @confirm="searchStore" />
			<uni-table   ref="table" border stripe style="margin-top: 0px;"  >
				<uni-tr>					
					<uni-th  width="80" align="center">店铺名称</uni-th>					
					<uni-th  width="40" align="center">数量</uni-th>
					<uni-th width="70"  align="center">成本金额</uni-th>
					<uni-th width="80"  align="center">估算销售金额</uni-th>
					<uni-th width="70" align="center">报表</uni-th>
				</uni-tr>
				<uni-tr v-for="(item, index) in inventoryStore" :key="index">					
					<uni-td align="center">{{ item.chainName }}</uni-td>
					<uni-td align="center" >{{ item.quantity }}</uni-td>		
					<uni-td align="center">{{ item.costAmount }}</uni-td>
					<uni-td align="center">{{ item.salesAmount }}</uni-td>
					
					<uni-td align="center">
						<view class="uni-group">
							
								<!-- <uni-icons type="redo" size="25" @click="details(item.id)"></uni-icons> -->

							<button size="mini" type="primary" style="height: 25px;width: 50px;background-color: rgb(0, 122, 255)" @click="details(item.id,item.chainName )" icon='shop'>
								<text style="font-size: 12px; position: absolute; left: 12px;top: -1px;">品牌</text>
							</button>
							<button size="mini" type="primary" style="height: 25px;width: 50px;background-color: palevioletred" @click="details(item.id,item.chainName )" icon='shop'>
								<text style="font-size: 12px; position: absolute; left: 12px;top: -1px;">类别</text>
							</button>
						</view>
					</uni-td>
					</uni-tr>
				</uni-table>

			

		
	</view>
</template>

<script>
	import getdateTime from '@/common/getdateTime.js'
	import {baseUrl} from 'libs/util/global'
	export default {
		data() {
			return {
				isSort: true,
				status: 'more',
				statusTypes: [{
					value: 'more',
					text: '加载前',
					checked: true
				}, {
					value: 'loading',
					text: '加载中',
					checked: false
				}, {
					value: 'noMore',
					text: '没有更多',
					checked: false
				}],
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				isAdmin: true,
				tableData: [],
				store: [], // 连锁店下拉框
				handler: [], // 经手人下拉框
				inventoryStore: [
					
				], // 连锁店信息
				// 表单数据
				baseFormData: {
					chainStore: '',
					handledBy: '',
					documentTime: []
				},
				rules: {
					chainStore: {
						rules: [{
							required: true,
							errorMessage: '请选择连锁店',
							ValidityState: 'submit'
						}, ]
					},				
					documentTime: {
						rules: [{
							required: true,
							errorMessage: '请选择单据日期',
							ValidityState: 'submit'
						}, ]
					},
				},
				loginUserId: null,
				loginUserType: null,
				authority: null
			}
		},
		mounted() {
			this.getStorage(),
			this.selectInventory()
		},
		onLoad(option) {
			uni.getStorage({
				key: 'userId',
				success:(res)=> {
					 // console.log(res.data + "登录用户id:"+res.data) 
					this.loginUserId = res.data
				}
			});
			uni.getStorage({
				key: 'isChain',
				success:(res) =>{
					 // console.log(res.data + "登录用户类型:"+res.data) 
					this.loginUserType = res.data
				}
			});
			uni.getStorage({
				key: 'authority',
				success:(res) =>{
					 // console.log(res.data + "登录用户权限:"+res.data) 
					this.authority  = res.data
				}
			});
			var timeStr = getdateTime.dateTimeStr('y-m-d')
			this.baseFormData.documentTime.push(timeStr, timeStr)

		},

		methods: {
			onBackPress() {
						// #ifdef APP-PLUS
						plus.key.hideSoftKeybord();
						// #endif
					},
					// 查询连锁店库存信息
			selectInventory(){
				uni.showLoading({
				title: '加载中.....'
				})
					 // console.log(res.value) 
					// 查看连锁店
					let selectInventoryStoreUrl = baseUrl+'/chain/inventory/'+ this.loginUserType+'/'+ this.loginUserId
					uni.request({
						url: selectInventoryStoreUrl,
						header: {'content-type': 'application/x-www-form-urlencoded'},
						method: 'POST',
						data: {},
						success: (res) => {
							setTimeout(function() {
								uni.hideLoading();
							}, 100);
							if (res.data.data != null) {
								// 拿到连锁店下拉框信息
								 console.log(res.data.data) 
									if(this.authority == 2){
										console.log("// 连锁店信息"+res.data)
										for( let index = 0; index< res.data.data.length; index++){
											console.log(res.data.data[index])
											res.data.data[index].costAmount = "0.00"
										}								
										this.inventoryStore = res.data.data		
									}else{
										this.inventoryStore = res.data.data	
									}
							} else {	
								uni.showToast({
								title: '暂无库存统计信息！',
								icon: 'none',
								duration: 500
									});
							}
						}
					})
			},		
			// 模糊搜索连锁店店铺
			searchStore(res) {
						uni.showLoading({
						title: '加载中.....'
						})
							 // console.log(res.value) 
							// 查看连锁店
							let selectInventoryStoreUrl = baseUrl+'/chain/likeInventory'
							uni.request({
								url: selectInventoryStoreUrl,
								header: {'content-type': 'application/json'},
								method: 'POST',
								data: {
									storeName: res.value,
									userId: this.loginUserId,
									userType: this.loginUserType
								},
								success: (res) => {
									setTimeout(function() {
										uni.hideLoading();
									}, 100);
									if (res.data.data != null) {
										// 拿到连锁店下拉框信息
										 // console.log(res.data) 
										this.inventoryStore = res.data.data			
									} else {	
										uni.showToast({
										title: '暂无库存统计信息！',
										icon: 'none',
										duration: 500
											});
									}
								}
							})
						},
						
						
			// 左上角返回按钮
				back() {
				uni.switchTab({
					url: '/pages/work/work'
					})
				},
				
				backWork(){
					uni.switchTab({
						url: '/pages/work/work'
						})
				},

			// 获取连锁店数据
				getStorage() {
					uni.showLoading({
					title: '加载中.....'
					})
					 // console.log("inventory页面权限："+this.authority) 
					if(this.authority == 2){   //店员查看时成本金额为 0
					let selectInventoryStoreUrl = baseUrl+'/chain/inventory/' +this.loginUserType + "/" + this.loginUserId
					uni.request({
						url: selectInventoryStoreUrl,
						header: {'content-type': 'application/x-www-form-urlencoded'},
						method: 'GET',
						data: {},
						success: (res) => {
							setTimeout(function() {
								uni.hideLoading();
							}, 100);
							 // console.log(res.data) 
							if (res.data.data != null) {								
								 // console.log("// 连锁店信息"+res.data) 
								for( let index = 0; index< res.data.data.length; index++){
									 // console.log(res.data.data[index]) 
									res.data.data[index].costAmount = "0.00"
								}								
								this.inventoryStore = res.data.data			
							} else {	
								 // console.log("进入权限2") 
								uni.showToast({
								title: '暂无库存统计信息！',
								icon: 'none',
								duration: 500
									});
							}
						}
					})
					}else{
						// 查看连锁店
						let selectInventoryStoreUrl = baseUrl+'/chain/inventory/' +this.loginUserType + "/" + this.loginUserId
						uni.request({
							url: selectInventoryStoreUrl,
							header: {'content-type': 'application/x-www-form-urlencoded'},
							method: 'GET',
							data: {},
							success: (res) => {
								setTimeout(function() {
									uni.hideLoading();
								}, 100);
								if (res.data.data != null) {
									this.inventoryStore = res.data.data			
								} else {	
									uni.showToast({
									title: '暂无库存统计信息！',
									icon: 'none',
									duration: 500
										});
								}
							}
						})
					}
				
						},
				// 进入详情
				details(storeId,chainStoreName) {
					console.log("店铺名称："+chainStoreName)
					// console.log("库存统计："+storeId)									
					uni.navigateTo({
						url: '/pages/form/inventory/yearInventory?storeId=' + storeId + '&chainStoreName=' + chainStoreName
					})
				}
			
		}
	}
</script>

<style>
.chat-custom-right {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-end;
	}

	.chat-custom-text {
		font-size: 12px;
		color: #999;
	}

	.slot-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}

	.slot-image {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		margin-right: 10px;
		width: 30px;
		height: 30px;
	}

	.slot-text {
		flex: 1;
		font-size: 14px;
		color: #4cd964;
		margin-right: 10px;
	}


	.content {
		width: 100vw;
		height: 100vh;
	}

	.table-header {
		position: sticky;
		left: 0;
		top: 0;
		z-index: 1;
		background-color: lavenderblush;
	}

	//冻结表头第一列
	/deep/ .uni-table-tr .uni-table-td:first-child {
		position: sticky;
		left: 0;
		top: 0;
		background-color: lavenderblush;				
		z-index: 1;
	}

	/deep/ .uni-table-tr {
		overflow: visible;
		background-color: #fff;
	}

	/deep/ .uni-table-tr .uni-table-th:first-child {
		position: sticky;
		left: 0;
		top: 0;
		background-color: #fff;
		z-index: 1;
	}
</style>
